package com.wendu.oa.service.info.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wendu.oa.WdOaApp;
import com.wendu.oa.common.page.PageUtils;
import com.wendu.oa.common.page.QueryAction;
import com.wendu.oa.dao.info.EmpDao;
import com.wendu.oa.dto.EmployeeDto;
import com.wendu.oa.exception.BusinessException;
import com.wendu.oa.model.Employee;
import com.wendu.oa.service.info.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
@CacheConfig(cacheNames = "service.info.emp")
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpDao empDao;

    @Cacheable(key = "targetClass +'.'+ methodName+'(' + #p0 +')'")
    @Override
    public List<Employee> getEmpList(EmployeeDto employeeDto) {


        return empDao.findEmpList(employeeDto);
    }

    @Cacheable(key = "targetClass +'.'+ methodName+'(' + #p0 +')'")
    @Override
    public Map<String, Object> getEmpPage(EmployeeDto employeeDto) {

        return PageUtils.getPage(employeeDto,()-> empDao.findEmpList(employeeDto));

    }

    @CacheEvict(allEntries = true)
    @Override
    public void addEmp(EmployeeDto employeeDto) {
        empDao.insertEmp(employeeDto);
    }

    @CacheEvict(allEntries = true)
    @Override
    public void updateEmp(EmployeeDto employeeDto) {
        empDao.updateEmp(employeeDto);
    }

    @CacheEvict(allEntries = true)
    @Override
    public void deleteEmp(String eid) {
        empDao.deleteEmp(eid);
    }

    @CacheEvict(allEntries = true)
    @Override
    public void deleteEmp(String[] eids) {
        if(eids == null || eids.length==0){
            return;
        }
        for(String eid : eids){
            empDao.deleteEmp(eid);
        }
    }

    //管理表id
    private int mgrId;

    @PostConstruct //spring容器托管对象的构造方法执行后，立即执行的初始化方法
    public void init(){
        mgrId = empDao.findMgrMaxId();
    }

    //获取新的管理表的id
    private synchronized int getNewMgrId(){
        return ++mgrId;
    }

    @CacheEvict(allEntries = true)
    @Override
    public void setLeader(String eid) {
        //查出员工所属部门编号
        Integer depId = empDao.findDepIdByEmpId(eid);
        if(depId == null){
            throw new BusinessException("该员工无所属部门，无法设置为部门领导！");
        }

        empDao.deleteOtherLeaders(depId);

        //生成领导表中新id
        int newMgrId = getNewMgrId();

        //将领导数据保存在领导表中
        empDao.insertMgr(newMgrId,eid,depId);
    }



    @CacheEvict(allEntries = true)
    @Override
    public void createUser(String eid) {
        empDao.insertNewUser(eid);
    }
}
